package com.vpen.clib.base.comtroller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vpen.clib.base.entity.BaseEntity;
import com.vpen.clib.base.service.BaseService;
import com.vpen.clib.result.CResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.List;

import static com.vpen.clib.result.CResult.success;

/**
 * 描述
 *
 * @author 韦鹏
 * @date 2022/8/5 17:25
 */

public class BaseControllerImpl<Svc extends BaseService<Entity>, Entity extends BaseEntity> implements BaseController<Entity> {

    @Autowired
    private Svc service;

    @Override
    public Svc getBaseService() {
        return this.service;
    }

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param entity 查询实体
     * @return 所有数据
     */
    @Override
    public CResult<Page<Entity>> selectAll(Page<Entity> page, Entity entity) {
        return success(this.getBaseService().page(page, new QueryWrapper<>(entity)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    public CResult<Entity> selectOne(@PathVariable Serializable id) {
        return success(this.getBaseService().getById(id));
    }

    /**
     * 新增数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
    @Override
    public CResult<Boolean> insert(@RequestBody Entity entity) {
        return success(this.getBaseService().save(entity));
    }

    /**
     * 修改数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
    @Override
    public CResult<Boolean> update(@RequestBody Entity entity) {
        return success(this.getBaseService().updateById(entity));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Override
    public CResult<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return success(this.getBaseService().removeByIds(idList));
    }

}
